package com.jeeplus.modules.flowable.CheckCycle.web;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.flowable.CheckCycle.Service.BasicdataCheckStandardMainCycleService;
import com.jeeplus.modules.flowable.CheckCycle.entity.BasicdataCheckStandardMainCycle;
import com.jeeplus.modules.flowable.checkPlanTable.entity.CheckPlanTable;
import com.jeeplus.modules.flowable.checkPlanTable.service.CheckPlanTableService;
import com.jeeplus.modules.flowable.entity.Flow;
import com.jeeplus.modules.flowable.service.FlowTaskService;
import com.jeeplus.modules.flowable.web.FlowableTaskController;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;


@RestController
@RequestMapping("/flowable/CheckCycle")
public class MyCheckCycleController extends BaseController {

    @Autowired
    BasicdataCheckStandardMainCycleService basicdataCheckStandardMainCycleService;

    @Autowired
    FlowTaskService flowTaskService;

    @Autowired
    TaskService taskService;

    @Autowired
    CheckPlanTableService checkPlanTableService;

    @PostMapping("Save")
    public AjaxJson save(String id) throws Exception{
        BasicdataCheckStandardMainCycle basicdataCheckStandardMainCycle = basicdataCheckStandardMainCycleService.getid(id);
        String[] split = basicdataCheckStandardMainCycle.getBasicdataCheckJob().split(",");
        CheckPlanTable checkPlanTable = new CheckPlanTable();
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        String format = formatter.format(date);
        checkPlanTable.setStartTime(format);
        checkPlanTable.setBasicdataCheckId(id);
        checkPlanTableService.save(checkPlanTable);

        //指定下一步处理人
        for (int i = 0; i < split.length; i++) {
            if(split[i].equals("1")){
                String saveID = basicdataCheckStandardMainCycleService.saveID(basicdataCheckStandardMainCycle);
                FlowableTaskController flowableTaskController = new FlowableTaskController();
                String procInsId = flowTaskService.startProcess("Process_1604919255612", "check_standard_cycle", saveID, "周期点检");
                if(StringUtils.isNotBlank(basicdataCheckStandardMainCycle.getBasicdataCheckCheckPeople().getId())){
                    Task task = taskService.createTaskQuery().processInstanceId(procInsId).active().singleResult();
                    if(task != null){
                        taskService.setAssignee(task.getId(), basicdataCheckStandardMainCycle.getBasicdataCheckCheckPeople().getId());
                    }
                }
            }
            if(split[i].equals("2") ){
                String saveID = basicdataCheckStandardMainCycleService.saveID(basicdataCheckStandardMainCycle);
                FlowableTaskController flowableTaskController = new FlowableTaskController();
                String procInsId = flowTaskService.startProcess("Process_1604919255612", "check_standard_cycle", saveID, "周期检修");
                if(StringUtils.isNotBlank(basicdataCheckStandardMainCycle.getBasicdataCheckFixPeople().getId())){
                    Task task = taskService.createTaskQuery().processInstanceId(procInsId).active().singleResult();
                    if(task != null){
                        taskService.setAssignee(task.getId(), basicdataCheckStandardMainCycle.getBasicdataCheckFixPeople().getId());
                    }
                }
            }
            if(split[i].equals("3")){
                String[] split1 = basicdataCheckStandardMainCycle.getBasicdataCheckPostPeople().getId().split(",");
                for (int j = 0; j < split1.length; j++) {
                    String saveID = basicdataCheckStandardMainCycleService.saveID(basicdataCheckStandardMainCycle);
                    FlowableTaskController flowableTaskController = new FlowableTaskController();
                    String procInsId = flowTaskService.startProcess("Process_1604919255612", "check_standard_cycle", saveID, "周期岗位");
                    if(StringUtils.isNotBlank(split1[j])){
                        Task task = taskService.createTaskQuery().processInstanceId(procInsId).active().singleResult();
                        if(task != null){
                            taskService.setAssignee(task.getId(), split1[j]);
                        }
                    }
                }

            }
        }
        return AjaxJson.success("启动流程成功");
    }


    public String saveCheckQuartz(String id) throws Exception{
        BasicdataCheckStandardMainCycle basicdataCheckStandardMainCycle = basicdataCheckStandardMainCycleService.getid(id);
        String[] split = basicdataCheckStandardMainCycle.getBasicdataCheckJob().split(",");
        CheckPlanTable checkPlanTable = new CheckPlanTable();
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        String format = formatter.format(date);
        checkPlanTable.setStartTime(format);
        checkPlanTable.setBasicdataCheckId(id);
        checkPlanTableService.save(checkPlanTable);
        String procInsId = "";
        //指定下一步处理人
        for (int i = 0; i < split.length; i++) {
            if(split[i].equals("1")){
                String saveID = basicdataCheckStandardMainCycleService.saveID(basicdataCheckStandardMainCycle);
                FlowableTaskController flowableTaskController = new FlowableTaskController();
                procInsId = flowTaskService.startProcess("Process_1604919255612", "check_standard_cycle", saveID, "周期点检");
                if(StringUtils.isNotBlank(basicdataCheckStandardMainCycle.getBasicdataCheckCheckPeople().getId())){
                    Task task = taskService.createTaskQuery().processInstanceId(procInsId).active().singleResult();
                    if(task != null){
                        taskService.setAssignee(task.getId(), basicdataCheckStandardMainCycle.getBasicdataCheckCheckPeople().getId());
                    }
                }
            }
        }
        return procInsId;
    }
}
